package myauctioneer.controller;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.http.*;

import myauctioneer.dto.Article;
import myauctioneer.dto.User;
import myauctioneer.service.ArticleService;

/**
 * Servlet implementation class SalesServlet
 */
@javax.servlet.annotation.WebServlet("/article/*")
public class ArticleServlet extends DefaultServlet {
	private static final long serialVersionUID = 1L;

	public void showSalesAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		if ((new myauctioneer.dao.ArticleDAO()).loadArticlesByUserId(user
				.getUserId()) != null) {
			request.setAttribute("aAL", (new myauctioneer.dao.ArticleDAO())
					.loadArticlesByUserId(user.getUserId()));
			request.setAttribute("template", "showSales.jsp");
		} else {
			request.setAttribute("template", "showSales.jsp");
		}
	}

	public void createArticleAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String brand, familyLine, model, condition, description, createdAt, validUntil, category, price, saleType, userId;

		long now = System.currentTimeMillis();
		Date date = new Date(now);
		SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
		createdAt = formatter.format(date);

		brand = request.getParameter("brand");
		familyLine = request.getParameter("familyLine");
		model = request.getParameter("model");
		condition = request.getParameter("condition");
		description = request.getParameter("description");
		validUntil = request.getParameter("validUntil");
		category = request.getParameter("category");
		price = request.getParameter("price");
		saleType = request.getParameter("saleType");
		userId = request.getParameter("userId");

		if ((new myauctioneer.service.ArticleService()).createArticle(brand,
				familyLine, model, condition, description, createdAt,
				validUntil, category, price, saleType, userId)) {
			request.setAttribute("title", "Article created");
			request.setAttribute("content", "The article is now created.");
			request.setAttribute("template", "success.jsp");
		} else {
			request.setAttribute("template", "createArticle.jsp");
		}
	}

	public void showPurchasesAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		if ((new myauctioneer.dao.ArticleDAO())
				.loadArticlesByUserIdWithInvalidDate(user.getUserId()) != null) {
			request.setAttribute("aAL", (new myauctioneer.dao.ArticleDAO())
					.loadArticlesByUserIdWithInvalidDate(user.getUserId()));
			request.setAttribute("template", "showPurchases.jsp");
		} else {
			request.setAttribute("template", "showPurchases.jsp");
		}
	}

	public void showStatisticsAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		int numberSoldArticles = 0;
		double turnover = 0;
		if ((new myauctioneer.dao.ArticleDAO()).loadArticlesByUserId(user.getUserId()) != null) {
			for (Article a : (new myauctioneer.dao.ArticleDAO()).loadArticlesByUserId(user.getUserId())) {
				numberSoldArticles++;
				turnover += a.getPrice();
			}
			request.setAttribute("numberSoldArticles", numberSoldArticles);
			request.setAttribute("turnover", turnover);
			request.setAttribute("template", "showStatistics.jsp");
		} else {
			request.setAttribute("template", "showStatistics.jsp");
		}
	}

	public void showAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt((String) request.getParameter("id"));
		Article a = (new myauctioneer.dao.ArticleDAO())
				.loadArticleByArticleId(id);

		myauctioneer.App.getLogger().info("Atricle: " + id);

		if (a == null) {
			this.notFoundAction(request, response);
			return;
		}

		request.setAttribute("article", a);
		request.setAttribute("template", "showArticle.jsp");
	}
}
